package com.example.automanagement.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.automanagement.dto.NameRoleDto;
import com.example.automanagement.entity.*;
import com.example.automanagement.mapper.UserMapper;
import com.example.automanagement.service.DriverArchService;
import com.example.automanagement.service.DriverStatusService;
import com.example.automanagement.service.UserRoleService;
import com.example.automanagement.service.UserService;
import com.example.automanagement.util.PageBean;
import com.example.automanagement.util.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class DriverArchController {
    @Resource
    private DriverArchService driverArchService;
    @Resource
    private DriverStatusService driverStatusService;

    @Resource
    private UserRoleService userRoleService;
    //接口有效性数据校验还都没加
    @Resource
    private UserService userService;

    public static final String  DRIVER_ROLE = "驾驶员";

    @GetMapping("/driverPageQuery")
    public Result driverPageQuery(){
        List<DriverArchives> archives= driverArchService.pageList();
        return Result.success(archives);
    }

    @DeleteMapping("/driverArchiveDelete")
    public Result driverArchiveDelete(@RequestParam("userId") String userId){
        DriverArchives  archive = driverArchService.getDriverArchiveById(userId);
        if(archive != null)
            //也要同步删除驾驶员状态
        {
            driverStatusService.deleteDriverStatus(userId);
            //完成状态的删除才能删除档案
            driverArchService.deleteDriverArchiveById(archive);
        }

        return Result.success(archive);
    }
    @PostMapping("/driverArchiveInsert")
    public Result driverArchiveInsert(@RequestBody DriverArchives archive){
        //从用户-角色表中查到是驾驶员角色的用户才能注册为驾驶员
        //下拉框选择工号（姓名）

        if(archive == null)
            return Result.error("500","请检查输入");
        //判断驾驶员档案注册姓名是否与个人账号上的姓名一致
        if(!(userService.getUserById(archive.getUserId())).getUserName().equals(archive.getDriverName()) )
            return Result.error("500","请检查输入");

        driverArchService.insertDriverArchive(archive);

        DriverStatus driverStatus = new DriverStatus(archive.getUserId(),"空闲");
        driverStatusService.addDriverStatus(driverStatus);

        return Result.success();
    }
    @PutMapping("/driverArchiveUpdate")
    public Result driverArchiveUpdate(@RequestBody DriverArchives archive){
        if(archive != null)
            driverArchService.updateDriverArchive(archive);
        return Result.success(archive);
    }
    @GetMapping("/getDriverList")
    public Result getDriverList(){
        List<DriverArchives> archives = driverArchService.getAllDrivers();
        return Result.success(archives);
    }

    @GetMapping("/getDriverNameById")
    public Result getDriverNameById(@RequestParam("driverId") String driverId){
        //根据驾驶员id获取名字
        if(driverId == null)
            return Result.error("500","请检查输入");
        DriverArchives  archive = driverArchService.getDriverArchiveById(driverId);
        return Result.success(archive.getDriverName());

    }
    //用于新建驾驶员档案
    @GetMapping("/getAvailableDriverRole")
    public Result getAvailableDriverRole(){

        List<UserRole> users = userRoleService.getIdByRole(DRIVER_ROLE);
        if(users == null)
            return Result.error("500","无可用角色");

        List<NameRoleDto> nameRoleDtos = BeanUtil.copyToList(users, NameRoleDto.class);
        for(int i = 0; i < users.size(); i++){
            String userId = users.get(i).getUserId();

            User user = userService.getUserById(userId);

            if(user == null)
                return Result.error("500","无可用角色");

            nameRoleDtos.get(i).setUserName(user.getUserName());
        }

        return Result.success(nameRoleDtos);
    }

    //根据驾驶员ID更新驾驶员状态，将其更改为“空闲”
    @PutMapping("/updateDriverStatusFree")
    public Result updateDriverStatusFree(@RequestParam("driverId") String driverId){
        if(driverId == null)
            return Result.error("500", "不存在对应驾驶员 ");
        driverStatusService.updateDriverFree(driverId);
        return Result.success();
    }
}
